package com.briup.day22;

import java.util.Scanner;

/**
 * @author ThinkBook
 */
public class Test01_Array {
    public static void main(String[] args) {
        int[] arr = {6,3,2,1,4};

        //2.从键盘获取 数组索引值
        Scanner sc = new Scanner(System.in);
        System.out.println("input index: ");
        int index = sc.nextInt();

        //3.获取 索引位置上的值【如果数组越界，主动捕获异常】
        try {
            // ClassCastException
            Object obj = new Object();
            String s1 = (String)obj;

            // ArithmeticException
            int a = 10 / 0;

            String str = "123a";
            // NumberFormatException
            int num = Integer.parseInt(str);
            System.out.println("num: " + num);

            //数组越界异常
            int value = arr[index];
            System.out.println("value: " + value);
        }catch(Exception e) {
            e.printStackTrace();
        }

        System.out.println("after catch ...");
    }

    // 1个catch块 捕获 多种类型异常
    public static void main02(String[] args) {
        int[] arr = {6,3,2,1,4};

        //2.从键盘获取 数组索引值
        Scanner sc = new Scanner(System.in);
        System.out.println("input index: ");
        int index = sc.nextInt();

        //3.获取 索引位置上的值【如果数组越界，主动捕获异常】
        try {
            // ClassCastException
            Object obj = new Object();
            String s1 = (String)obj;

            // ArithmeticException
            int a = 10 / 0;

            String str = "123a";
            // NumberFormatException
            int num = Integer.parseInt(str);
            System.out.println("num: " + num);

            //数组越界异常
            int value = arr[index];
            System.out.println("value: " + value);
        }catch(ArrayIndexOutOfBoundsException |
                NumberFormatException |
                ArithmeticException e) {
            e.printStackTrace();
        }

        System.out.println("after catch ...");
    }

    // 主动捕获 各种异常 多个catch块
    public static void main01(String[] args) {
        int[] arr = {6,3,2,1,4};

        //2.从键盘获取 数组索引值
        Scanner sc = new Scanner(System.in);
        System.out.println("input index: ");
        int index = sc.nextInt();

        //3.获取 索引位置上的值【如果数组越界，主动捕获异常】
        try {
            // ClassCastException
            Object obj = new Object();
            String s1 = (String)obj;

            // ArithmeticException
            int a = 10 / 0;

            String str = "123a";
            // NumberFormatException
            int num = Integer.parseInt(str);
            System.out.println("num: " + num);

            //数组越界异常
            int value = arr[index];
            System.out.println("value: " + value);
        }catch(ArrayIndexOutOfBoundsException e) {
            //catch只能捕获 声明类型的异常，如果出现的异常未声明，则无法捕获，
            // 按照默认处理方式【向上一级传递】

            //主动处理异常：输出异常信息
            e.printStackTrace();
        }catch(NumberFormatException e) {
            e.printStackTrace();
        }catch(ArithmeticException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("after catch ...");
    }

}
